package com.css.fxfzaqbz.modules.assess.repository.impl;

import com.css.fxfzaqbz.constants.YNEnum;
import com.css.fxfzaqbz.modules.assess.entity.EarthEntity;
import com.css.fxfzaqbz.modules.assess.repository.EarthEntityRepository;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;

/**
 * @author y1fan
 * @create 2022-09-16 14:50
 */
@Repository
public class EarthEntityRepositoryImpl implements EarthEntityRepository {



    @Resource
    JdbcTemplate jdbcTemplate;
    @Resource
    NamedParameterJdbcTemplate namedParameterJdbcTemplate;


    @Override
    public void saveEntity(EarthEntity earthEntity) {
        String sql = "insert into sys_earth_assess  (ee_id,ee_activeid,ee_eeopinion,ee_status,ee_createtime,ee_del_flag,ee_createuser,content)values(:eeId,:eeActiveId,:eeOpinion,:eeStatus,:eeCreateTime,:eeDelFlag,:eeCreateUser,:content)";

        SqlParameterSource source = new BeanPropertySqlParameterSource(earthEntity);
        //拿到新插入的主键值（i）
        KeyHolder keyHolder = new GeneratedKeyHolder();
        int i = namedParameterJdbcTemplate.update(sql, source, keyHolder);

    }

    @Override
    public EarthEntity getEarthByActiveId(String sraRelation) {
        String sql="";
        sql="select u.* from sys_earth_assess u  where  u.ee_activeid='"+sraRelation+"' and u.ee_del_flag = '"+ YNEnum.N.toString() +"'";
        EarthEntity earthEntity = null;
        try {
            earthEntity = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(EarthEntity.class));
            return earthEntity;
        } catch (DataAccessException e) {
            return null;
        }

    }

    @Override
    public void updateBigStatus(String bbId) {
        String sql = "update sys_earth_assess set" +
                " ee_status = '"+YNEnum.Y+"' " +
                " where ee_id = '" + bbId + "'";
        jdbcTemplate.update(sql);
    }

    @Override
    public void upStatus(String id) {
        String sql = "update sys_earth_assess set" +
                " ee_status = '"+2+"' " +
                " where ee_activeid = '" + id + "'";
        jdbcTemplate.update(sql);
    }
}
